﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using BusinessObject;
using Utility;
using System.Resources;
namespace WebTeamplate
{

    public partial class Admin_ThongKeTonKho : BasePage
    {
        public string SortField
        {

            get
            {
                object o = ViewState["SortField"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["SortField"] = value; }

        }
        public string SortExpression
        {

            get
            {
                object o = ViewState["SortExpression"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["SortExpression"] = value; }

        }
        public string data = string.Empty;
        #region Init,Load
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientDefaultButton.SetPageDefaultButton(this.Page, btnThongKe.ClientID);
            if (!Page.IsPostBack)
            {
                Session["tonkho"] = string.Empty;
                InitData();
                CommonFunction.LoadSubCate(drlChungXe, CommonFunction.DEFAULT_CHUNGLOAIXE_CODE, true);
                CommonFunction.LoadSubCate(drlKho, CommonFunction.DEFAULT_WAREHOUSE_CODE, true);
                CommonFunction.LoadSubCate(drlLoaiMH, CommonFunction.DEFAULT_LOAIHANGHOA_CODE, false);
                LoadDMXe();
                //LoadMHData();
                ThongKe();
                drlKho.Focus();
            }
        }
        private void LoadDMXe()
        {
            List<DMXeResult> lstResult = DMXeUtil.GetByChungXe(ConvertValueObject.ParseInt(drlChungXe.SelectedValue));
            drlXe.Items.Clear();
            ListItem nullItem = new ListItem(BaseFunctions.COMBOBOX_NULL_VALUE, string.Empty);
            drlXe.Items.Add(nullItem);
            foreach (DMXeResult objXe in lstResult)
            {
                drlXe.Items.Add(new ListItem(objXe.TenXe, objXe.MaXe.ToString()));
            }
        }
        protected void drlChungXe_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDMXe();            
            ThongKe();
        }
     
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitTextControl();
        }
        #endregion

        #region Methods
        public void InitTextControl()
        {

            btnReset.Text = Resources.Resource.LABEL_BUTTON_RESET;
        }
        public int PageIndex
        {

            get
            {

                object o = ViewState["PageIndex"];

                if (o != null) return (int)o;

                return 0;

                //a default

            }

            set { ViewState["PageIndex"] = value; }

        }
        private void InitData()
        {
            this.PageIndex = 1;
            Pager1.PageSize = CommonFunction.DEFAULT_ADMIN_PAGE_SIZE;
            SortField = "tk.MaMatHang";
            SortExpression = "ASC";
        }

        #endregion

        #region Events,Handler
        protected void Pager1_PageIndexChanging(object obj, PagerControl.PageIndexChangeEventArgs e)
        {
            this.PageIndex = e.newPageIndex;
            ThongKe();
        }
        protected void Pager1_PageSizeChanging(object obj, PagerControl.PageSizeChangeEventArgs e)
        {
            this.PageIndex = 1;
            //  this.PageSize = e.newPageSize;
            ThongKe();
        }
        private void ThongKe()
        {
            int total = 0;
            int selectRow = 0;
            List<ChiTietTonKho> lstResult = MatHangTonKhoUtil.GetThongKeTonKho(ConvertValueObject.ParseInt(drlKho.SelectedValue), ConvertValueObject.ParseInt(drlChungXe.SelectedValue), ConvertValueObject.ParseInt(drlXe.SelectedValue), txtMaMatHang.Text.Trim(), txtMaPhuTung.Text.Trim(), SortField, SortExpression, this.PageIndex, Pager1.PageSize, ref total);
            if (lstResult != null && lstResult.Count > 0)
            {
                selectRow = lstResult.Count;
                string result = @"
                <table width='100%' cellspacing='1' cellpadding='3' border='0' class='lvt small'>
                        
                            <tr>
                                <td class='lvtCol' style='width: 40px;'>
                                    STT
                                </td>
                                <td class='lvtCol' style='width: 120px;'>
                                    <input type='image' style='border-width: 0px;' src='../App_Themes/Admin/images/icon_sort_d_new.gif' onClick=SortOrder('tk.MaMatHang','DESC') id='imgMaMHSortDown' id='imgMaMHSortDown'>
                                  Mã MH
                                    <input type='image' style='border-width: 0px;' src='../App_Themes/Admin/images/icon_sort_a_new.gif' onClick=SortOrder('tk.MaMatHang','ASC') id='imgMaMHSortUp' id='imgMaMHSortUp'>
                                </td>
                                <td class='lvtCol'>
                                    <input type='image' style='border-width: 0px;' src='../App_Themes/Admin/images/icon_sort_d_new.gif' onClick=SortOrder('mh.TenMatHang','DESC') id='imgTenMHSortDown' id='imgTenMHSortDown'>
                                  Tên MH
                                    <input type='image' style='border-width: 0px;' src='../App_Themes/Admin/images/icon_sort_a_new.gif' onClick=SortOrder('mh.TenMatHang','ASC') id='imgTenMHSortUp' id='imgTenMHSortUp'>
                                </td>
                                <td class='lvtCol' style='width: 130px;'>
                                    <input type='image' style='border-width: 0px;' src='../App_Themes/Admin/images/icon_sort_d_new.gif' onClick=SortOrder('cx.SubCategoryName','DESC') id='imgChungXeSortDown' id='imgChungXeSortDown'>
                                   Chủng xe
                                    <input type='image' style='border-width: 0px;' src='../App_Themes/Admin/images/icon_sort_a_new.gif' onClick=SortOrder('cx.SubCategoryName','ASC') id='imgChungXeSortUp' id='imgChungXeSortUp'>
                                </td>
                                <td class='lvtCol' style='width: 130px;'>
                                   <input type='image' style='border-width: 0px;' src='../App_Themes/Admin/images/icon_sort_d_new.gif' onClick=SortOrder('x.TenXe','DESC') id='imgXeSortDown' id='imgXeSortDown'>
                                   Xe
                                    <input type='image' style='border-width: 0px;' src='../App_Themes/Admin/images/icon_sort_a_new.gif' onClick=SortOrder('x.TenXe','ASC') id='imgXeSortUp' id='imgXeSortUp'>
                                </td>
                                <td class='lvtCol' style='width: 80px;'>
                                    Số lượng tồn
                                </td>
                                <td class='lvtCol' style='width: 80px;'>
                                   Đơn vị tính
                                </td>
                               
                            </tr>";
                int count = lstResult.Count;
                
                while (count > 0)
                {
                    ChiTietTonKho objChiTiet = lstResult[0];
                  //  if (objChiTiet.Type == 1)//loaiMH
                   // {
                        //lstResult.Remove(objChiTiet);
                        result += GenerateByGroup(ref lstResult, objChiTiet);
                  //  }
                    count = lstResult.Count;
                }
                result += "</table>";
                data = result;
                Session["tonkho"] = data;
                
            }
            Pager1.SetPaging(this.PageIndex, selectRow, total);
        }
        protected void btnSortOrder_Click(object sender, EventArgs e)
        {
            SortField = hidSortField.Value;
            SortExpression = hidSortExpression.Value;
            ThongKe();
        }
        
        protected void btnThongKe_Click(object sender, EventArgs e)
        {
            ThongKe();
        }
        private string GenerateByGroup(ref List<ChiTietTonKho> lstResult, ChiTietTonKho objData)
        {
            if (lstResult.Count <= 0)
                return string.Empty;
            string result = string.Empty;
            int i = 0;
            ChiTietTonKho p = lstResult[0];
            while (p.Id!="")
            {
                result += @"<tr bgcolor='white' class='lvtColData' onmouseover='this.className='lvtColDataHover'' onmouseout='this.className='lvtColData''>
                            <td class='repeaterCenterData'>
                                " + (i + 1).ToString() + @"
                            </td>
                            <td class='repeaterLeftData'>
                                " + p.Id + @"
                            </td>
                            <td class='repeaterLeftData'>
                               " + p.Name + @"
                            </td>
                            <td class='repeaterLeftData'>
                               " + p.TenChungXe + @"
                            </td>
                                <td class='repeaterLeftData'>
                               " + p.TenXe + @"
                            </td>
                            <td class='repeaterRightData'>
                               " + ConvertValueObject.ParseString(p.SoLuongTon) + @"
                            </td>
                            <td class='repeaterLeftData'>
                              " + p.DonViTinh + @"
                            </td>
                        </tr>";
                lstResult.Remove(p);
                i++;
                if (lstResult.Count > 0)
                    p = lstResult[0];
                else break;
            }
            if (result == string.Empty)
                return string.Empty;
//            result = @"<tr bgcolor='white' class='lvtColData' onmouseover='this.className='lvtColDataHover'' onmouseout='this.className='lvtColData''>
//                                <td style='padding-left: 45px;' colspan='7' class='repeaterLeftData'><b>
//                                " + objData.Name + @"</b>
//                                </td>
//                               
//                            </tr>" + result;
            return result;

        }
        #endregion
        protected void drlLoaiMH_SelectedIndexChanged(object sender, EventArgs e)
        {
            //LoadMHData();
        }
        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect("ThongKeTonKho.aspx");
        }
        protected void drlXe_SelectedIndexChanged(object sender, EventArgs e)
        {

            //LoadMHData();
            ThongKe();
        }
    }
}
